package com.cxb.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.util.Log;

/**
* @ClassName: WifiAPUtil
* @Description: wifiap 设置 
* @author 杨瑜 45154139@qq.com
* @date 2013-10-17 下午7:36:11
* 
*/
public class WifiAPUtil {

	public static final int WIFI_AP_STATE_DISABLING = 10;
	public static final int WIFI_AP_STATE_DISABLED = 11;
	public static final int WIFI_AP_STATE_ENABLING = 12;
	public static final int WIFI_AP_STATE_ENABLED = 13;
	public static final int WIFI_AP_STATE_FAILED = 14;

	/**
	* @Title: setWifiAp
	* @Description:  wifiap开关
	* @param wifiManager
	* @param ssid wifi名称
	* @param password wifi密码
	* @param state 需要设置的状态
	* @date  2013-10-17  下午7:36:54  
	* @author 陈伟斌
	*/
	public void setWifiAp(WifiManager wifiManager, String ssid,
			String password, boolean state) { 
		Method method1 = null;
		try {
			method1 = wifiManager.getClass().getMethod("setWifiApEnabled",
					WifiConfiguration.class, boolean.class);
			WifiConfiguration netConfig = new WifiConfiguration();
			// wifi热点名字
			netConfig.SSID = ssid;
			netConfig.allowedAuthAlgorithms
					.set(WifiConfiguration.AuthAlgorithm.OPEN);
			netConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
			netConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
			//如果没有密码或密码为空，设置网络为开放
			if (password == null || password.length() == 0) {
				netConfig.allowedKeyManagement
						.set(WifiConfiguration.KeyMgmt.NONE);
			} else {
				netConfig.allowedKeyManagement
						.set(WifiConfiguration.KeyMgmt.WPA_PSK);
			}

			netConfig.allowedPairwiseCiphers
					.set(WifiConfiguration.PairwiseCipher.CCMP);
			netConfig.allowedPairwiseCiphers
					.set(WifiConfiguration.PairwiseCipher.TKIP);
			netConfig.allowedGroupCiphers
					.set(WifiConfiguration.GroupCipher.CCMP);
			netConfig.allowedGroupCiphers
					.set(WifiConfiguration.GroupCipher.TKIP);
			// 密码
			netConfig.preSharedKey = password; 
			method1.invoke(wifiManager, netConfig, state); 
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	* @Title: getWifiApState
	* @Description:  获取wifiap状态
	* @param wifiManager
	* @return
	* @date  2013-10-17  下午7:40:18  
	* @author 陈伟斌
	*/
	public int getWifiApState(WifiManager wifiManager) {
		try {
			Method method = wifiManager.getClass().getMethod("getWifiApState");
			int i = (Integer) method.invoke(wifiManager);
			Log.e("ares", "wifi state:  " + i);
			return i;
		} catch (Exception e) {
			Log.e("ares", "Cannot get WiFi AP state" + e);
			return WIFI_AP_STATE_FAILED;
		}
	}
}